﻿using System;
using System.Windows.Data;

namespace BloodGlucoseManagerApplication
{
    [ValueConversion(typeof(string), typeof(double))]
    class ZoomConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            string ret = "";

            try
            {
                ret = String.Format("{0:#}", ((double)value * 100)) + "%";
            }
            catch
            {
                return null;
            }
            return ret;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            double ret;

            try
            {
                if (value.ToString() == "")
                {
                    return null;
                }
                else if (value.ToString().Contains("%"))
                {
                    ret = Double.Parse(value.ToString().Replace("%", "")) / 100;
                }
                else
                {
                    ret = Double.Parse(value.ToString()) / 100;
                }
            }
            catch (FormatException)
            {
                return null;
            }

            return ret;
        }
    }
}